package com.appcenter.monitor.service;

import com.appcenter.jz.config.JsonResult;
import com.appcenter.jz.page.PagedResult;
import com.appcenter.monitor.config.RemoteProperties;
import com.appcenter.monitor.dto.Apps;
import com.appcenter.monitor.expand.DicArray;
import com.appcenter.monitor.input.operats.GetOperatsInput;
import com.appcenter.monitor.input.operats.PageLoadInput;
import com.appcenter.monitor.mapper.OperatsMapper;
import com.appcenter.monitor.model.Operats;
import com.appcenter.monitor.output.operats.GetOperatsOutput;
import com.appcenter.monitor.output.operats.PageLoadOutput;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.appcenter.jz.unit.ObjectUnit.JsonUnit;
import static org.apache.http.util.TextUtils.isBlank;

@Service
public class OperatsService {
    @Autowired
    OperatsMapper operatsMapper;
    @Autowired
    RemoteProperties remoteProperties;

    @Autowired
    HttpAPIService httpAPIService;

    @Autowired
    ObjectMapper objectMapper;

    /**
     *  创建人:Coke
     *  备注: 获取操作日志
     *  时间: 2019/4/26 14:53 
     *  参数: 
     */
    public GetOperatsOutput GetOperats(GetOperatsInput input) {
        GetOperatsOutput output = new GetOperatsOutput();
        Example example = new Example(Operats.class);
        Example.Criteria criteria = example.createCriteria();
        if (!isBlank(input.getKeyword())) {
            criteria.andLike("message", "%" + input.getKeyword() + "%");
        }
        if (!isBlank(input.getAppCode())) {
            criteria.andEqualTo("appCode", input.getAppCode());
        }
        if (!isBlank(input.getLogType())) {
            criteria.andEqualTo("logType", input.getLogType());
        }
        if (!isBlank(input.getLevel())) {
            criteria.andEqualTo("level", input.getLevel());
        }
        if (!isBlank(input.getTerminal())) {
            criteria.andEqualTo("terminal", input.getTerminal());
        }
        criteria.andEqualTo("appCode", input.getAppCode());
        PageHelper.startPage(input.getPage().getPageNo(), input.getPage().getPageSize(), "id desc");
        List<Operats> ls = operatsMapper.selectByExample(example);
        PageInfo<Operats> page = new PageInfo<>(ls);
        output.setItems(new PagedResult<Operats>(page.getList(), (int) page.getTotal()));
        return output;

    }

    /**
     *  创建人:Coke
     *  备注: 加载操作日志
     *  时间: 2019/4/26 14:53 
     *  参数: 
     */
    public PageLoadOutput PageLoad(PageLoadInput input) throws IOException {
        PageLoadOutput output = new PageLoadOutput();

        List<DicArray> listTerminals=new ArrayList<DicArray>();
        DicArray terminals=new DicArray();
        terminals.setKey("PC");
        terminals.setText("电脑端");
        listTerminals.add(terminals);

        terminals=new DicArray();
        terminals.setKey("Mobile");
        terminals.setText("移动端");
        listTerminals.add(terminals);

        output.setTerminals(listTerminals);

        String GetAppsUrl = remoteProperties.getAppCenterServiceUrl() + "/app/getapps";
        String responseJson = httpAPIService.JsonSMS(GetAppsUrl, "{}");
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        JsonResult jsonResult = objectMapper.readValue(responseJson, JsonResult.class);
        List<Apps> apps = new ArrayList<Apps>();
        Object OneItems = JsonUnit(jsonResult.getResult(), "items");
        ArrayList<Object> TwoItems = (ArrayList<Object>) JsonUnit(OneItems, "items");
        if (TwoItems != null && TwoItems.size() > 0) {
            for (int i = 0; i < TwoItems.size(); i++) {
                Apps app = new Apps();
                app.setId(JsonUnit(TwoItems.get(i), "id").toString());
                app.setAppCode(JsonUnit(TwoItems.get(i), "appCode").toString());
                app.setAppName(JsonUnit(TwoItems.get(i), "appName").toString());
                apps.add(app);
            }
        }
        output.setApps(apps);

        output.setItems(GetOperats(input).getItems());
        return output;
    }
}
